<?php
/**
 * Created by PhpStorm.
 * 订单基础数据生成
 * User: caicm
 * Date: 2017/8/29
 * Time: 20:33
 */

namespace SDXApi\Controller;


use Think\Controller;

class OrderBasicsController extends Controller {

    //比率
    protected $rate = array(
        'a' => 5,       //7:00~10:00      5%
        'b' => 13,      //10:00~13:00     13%
        'c' => 10,      //13:00~16:00     10%
        'd' => 15,      //16:00~19:00     15%
        'e' => 25,      //19:00~21:00     25%
        'f' => 30,      //21:00~24:00     30%
        'g' => 2,       //0:00~7:00       2%

    );







    public function add_order_data(){
        set_time_limit(0);
        $year = I('year');


        for ($i=6; $i<=8; $i++){
            $this->add_order_to_month($year, $i);
        }
        echo 'ok';
    }




    /**
     * @param $year
     * @param $month
     */
    public function add_order_to_month($year, $month){

        $day = $this->get_month_day($year, $month);

        for ($m=1; $m<=$day; $m++){
            $result = array();
            for ($i=0; $i<10000; $i++){

                $date_info = $this->get_rand_date($year, $month, $m);

                $date_info['order_sn'] = $this->get_order_sn($date_info['add_time']);
                $date_info['order_date'] = date('YmdHis',$date_info['add_time']);
                $result[] = $date_info;
            }
            M('OrderBasics')->addAll($result);
        }

    }

    function get_order_sn($time) {

        return  date('YmdHis',$time). str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
    }


    /**
     * 获取随机时间
     * @param $year
     * @param $month
     * @param $day
     * @return array
     */
    protected function get_rand_date($year, $month, $day){
        $date = array(
            'a' => array('7:00','10:00'),       //7:00~10:00      5%
            'b' => array('10:00','13:00'),      //10:00~13:00     13%
            'c' => array('13:00','16:00'),      //13:00~16:00     10%
            'd' => array('16:00','19:00'),      //16:00~19:00     15%
            'e' => array('19:00','21:00'),      //19:00~21:00     25%
            'f' => array('21:00','24:00'),      //21:00~24:00     30%
            'g' => array('0:00','7:00'),       //0:00~7:00       2%
        );

        $key = $this->roll($this->rate);
        $res = $date[$key];


        return $this->get_rand_time($year, $month, $day, $res[0], $res[1]);

    }

    /**
     * 获取随机 下单时间 支付时间
     * @param $year
     * @param $month
     * @param $day
     * @param $begin_time
     * @param $end_time
     * @return array
     */
    protected function get_rand_time($year, $month, $day, $begin_time, $end_time){
        $date = $year.'-'.$month.'-'.$day.' ';
        $begin_time = strtotime($date.$begin_time);
        $end_time = strtotime($date.$end_time);

        //下单时间
        $add_time = mt_rand($begin_time, $end_time);

        //支付时间
        $pay_time = mt_rand($add_time+15, $add_time+60*15);

        return array(
            'add_time' => $add_time,
            'pay_time' => $pay_time
        );
    }

    /**
     * 获取随机值
     * @param array $weight 权重
     * @return string key 键名
     */
    protected function roll($weight = array()) {
        $roll = rand ( 1, array_sum ( $weight ) );
        $_tmpW = 0;
        $rollnum = 0;
        foreach ( $weight as $k => $v ) {
            $min     = $_tmpW;
            $_tmpW += $v;
            $max     = $_tmpW;
            if ($roll > $min && $roll <= $max) {
                $rollnum = $k;
                break;
            }
        }
        return $rollnum;
    }

    /**
     * 获取当月天数
     * @param string $year
     * @param string $month
     * @return false|string
     */
    protected function get_month_day($year = '', $month = ''){
        return date('t', strtotime(date($year .'-' . $month)));
    }
}